package com.cedarhd.brzx.domain.service;

import ime.service.client.SOAClient;
import ime.service.util.RSAUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * Created by lipei on 2018/4/26.
 */
@Component
public class Client {

    private Logger logger = LoggerFactory.getLogger(Client.class);

    private static SOAClient client;
    private static PublicKey publicKey;
    private static PrivateKey privateKey;
    //soa服务名
    private static String soaName = "MemberService";

    @Value("${tonglian.sysid}")
    public String sysid;

    @Value("${tonglian.soalocation}")
    public String soalocation;

    @Value("${tonglian.pwd}")
    public String pwd;

    @Value("${tonglian.alias}")
    public String alias;

    @Value("${tonglian.path}")
    public String path;

//    @Value("${tonglian.bizUserId}")
//    public String bizUserId;



    public SOAClient  getClient()  {

//        String serverAddress = "http://122.227.225.142:23661/service/soa";

        String signMethod = "SHA1WithRSA";

        try {
            System.out.println("sysid = " + sysid);

            System.out.println("pwd = " + pwd);

            System.out.println("alias = " + alias);

            System.out.println("path = " + path);


            privateKey = RSAUtil.loadPrivateKey(alias, path, pwd);

            publicKey = RSAUtil.loadPublicKey(alias, path, pwd);

            client = new SOAClient();
            client.setServerAddress(soalocation);
            client.setSignKey(privateKey);
            client.setPublicKey(publicKey);
            client.setSysId(sysid);
            client.setSignMethod(signMethod);

            logger.info("beforeClass success.");
            return client;
        } catch (Exception e) {
            logger.error("beforeClass error.");

            e.printStackTrace();
            return  null;
        }
    }


    public PrivateKey getPrivateKey(){
        try {
            privateKey = RSAUtil.loadPrivateKey(alias, path, pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return privateKey;

    }

    public PublicKey getPublicKey(){
        try {
            publicKey = RSAUtil.loadPublicKey(alias, path, pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  publicKey;

    }



}